package com.ruoyi.teaching.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.teaching.domain.Classes;
import com.ruoyi.teaching.domain.ExamDetail;
import com.ruoyi.teaching.domain.vo.ExamRecordBo;
import com.ruoyi.teaching.mapper.ClassesMapper;
import com.ruoyi.teaching.mapper.ExamDetailMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.ExamRecordMapper;
import com.ruoyi.teaching.domain.ExamRecord;
import com.ruoyi.teaching.service.IExamRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生成绩记录Service业务层处理
 *
 * @author sunshine
 * @date 2024-01-24
 */
@Service
public class ExamRecordServiceImpl implements IExamRecordService
{
    @Autowired
    private ExamRecordMapper examRecordMapper;
    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private ExamDetailMapper examDetailMapper;

    /**
     * 查询学生成绩记录
     *
     * @param id 学生成绩记录主键
     * @return 学生成绩记录
     */
    @Override
    public ExamRecord selectExamRecordById(Long id)
    {
        return examRecordMapper.selectExamRecordById(id);
    }

    /**
     * 查询学生成绩记录列表
     *
     * @param examRecord 学生成绩记录
     * @return 学生成绩记录
     */
    @Override
    public List<ExamRecord> selectExamRecordList(ExamRecord examRecord)
    {
        return examRecordMapper.selectExamRecordList(examRecord);
    }

    /**
     * 新增学生成绩记录
     *
     * @param examRecord 学生成绩记录
     * @return 结果
     */


    /**
     * 新增学生成绩
     *
     * @param examRecord 学生成绩
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertExamRecord(ExamRecordBo examRecord)
    {
        String username = SecurityUtils.getUsername();
        List<ExamDetail> examDetails = examRecord.getExamDetails();
        if(CollectionUtils.isEmpty(examDetails)){
            throw new ServiceException("至少录入一个学生的成绩");
        }
//        先新增一方就是重复数据的表数据
        Classes classes = classesMapper.selectClassesById(examRecord.getClassesId());
        examRecord.setClassesName(classes.getClassName());
        Double avgComputerScore = examDetails.stream()
                .filter(examDetail -> ObjectUtils.isNotEmpty(examDetail.getComputerScore()))
                .mapToInt(ExamDetail::getComputerScore)
                .average()
                .orElse(0);
        examRecord.setAvgComputerScore(avgComputerScore.intValue());

        Double avgWrittenScore = examDetails.stream()
                .filter(examDetail -> ObjectUtils.isNotEmpty(examDetail.getWrittenScore()))
                .mapToInt(ExamDetail::getWrittenScore)
                .average()
                .orElse(0);
        examRecord.setAvgWrittenScore(avgWrittenScore.intValue());
        examRecord.setCreateTime(new Date());
        examRecord.setCreateBy(SecurityUtils.getUsername());
        int rows = examRecordMapper.insertExamRecord(examRecord);
        if (rows == 0){
            throw new ServiceException("新增测试数据记录异常");
        }
        Long id = examRecord.getId();

//        再新增多方这边
        examDetails.forEach(examDetail -> {
            examDetail.setExamRecordId(id);
            examDetailMapper.insertExamDetail(examDetail);
        });



        return 1;
    }


    /**
     * 修改学生成绩记录
     *
     * @param examRecord 学生成绩记录
     * @return 结果
     */
    @Override
    public int updateExamRecord(ExamRecord examRecord)
    {
        examRecord.setUpdateTime(DateUtils.getNowDate());
        return examRecordMapper.updateExamRecord(examRecord);
    }

    /**
     * 批量删除学生成绩记录
     *
     * @param ids 需要删除的学生成绩记录主键
     * @return 结果
     */
    @Override
    public int deleteExamRecordByIds(Long[] ids)
    {
        return examRecordMapper.deleteExamRecordByIds(ids);
    }

    /**
     * 删除学生成绩记录信息
     *
     * @param id 学生成绩记录主键
     * @return 结果
     */
    @Override
    public int deleteExamRecordById(Long id)
    {
        return examRecordMapper.deleteExamRecordById(id);
    }


    @Override
    public Integer selectLastWeek(ExamRecord examRecord) {
        Integer result = examRecordMapper.selectLastWeek(examRecord);
        if (result == null) {
            result = 0;
        }
        return result;
    }


}
